package com.hexinfo.dmpro.component.comparison.conf;

import com.hexinfo.dmpro.component.comparison.constants.AsyncConstants;
import com.hexinfo.dmpro.component.comparison.constants.DataContrastAsyncConstants;
import com.hexinfo.dmpro.component.comparison.constants.DataReadAsyncConstants;
import com.hexinfo.dmpro.component.comparison.constants.TableContrastAsyncConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync()
public class AsyncTaskPoolConfig {

    private DataContrastAsyncConstants dataContrastAsyncConstants;

    private TableContrastAsyncConstants tableContrastAsyncConstants;

    private DataReadAsyncConstants dataReadAsyncConstants;

    @Autowired(required = false)
    public AsyncTaskPoolConfig(DataContrastAsyncConstants dataContrastAsyncConstants,
                               TableContrastAsyncConstants tableContrastAsyncConstants,
                               DataReadAsyncConstants dataReadAsyncConstants) {
        this.dataContrastAsyncConstants = dataContrastAsyncConstants;
        this.tableContrastAsyncConstants = tableContrastAsyncConstants;
        this.dataReadAsyncConstants = dataReadAsyncConstants;
    }

    /*
     * @Method: dataContrastAsyncTaskExecutor <br>
     * @Param: [] <br>
     * @Return: java.util.concurrent.Executor <br>
     * @Description：数据比对线程池<br>
     * @Author： wz.li<br>
     * @Date： 2023/9/27 16:19 <br>
     * @Version： V2.0.2<br>
     */
    @Bean(name = "dataContrastAsyncTaskExecutor")
    public Executor dataContrastAsyncTaskExecutor() {
        return initExecutor(dataContrastAsyncConstants, "dataContrastAsyncTaskExecutor-");
    }

    /*
     * @Method: tableContrastAsyncTaskExecutor <br>
     * @Param: [] <br>
     * @Return: java.util.concurrent.Executor <br>
     * @Description：表比对任务线程池<br>
     * @Author： wz.li<br>
     * @Date： 2023/9/27 16:19 <br>
     * @Version： V2.0.2<br>
     */
    @Bean(name = "tableContrastAsyncTaskExecutor")
    public Executor tableContrastAsyncTaskExecutor() {
        return initExecutor(tableContrastAsyncConstants, "tableContrastAsyncTaskExecutor-");
    }

    /*
     * @Method: dataReadAsyncTaskExecutor <br>
     * @Param: [] <br>
     * @Return: java.util.concurrent.Executor <br>
     * @Description：源表和目标表异步数据读取线程池<br>
     * @Author： wz.li<br>
     * @Date： 2023/9/27 16:19 <br>
     * @Version： V2.0.2<br>
     */
    @Bean(name = "dataReadAsyncTaskExecutor")
    public Executor dataReadAsyncTaskExecutor() {
        return initExecutor(dataReadAsyncConstants, "dataReadAsyncTaskExecutor-");
    }

    //初始化方法
    private ThreadPoolTaskExecutor initExecutor(AsyncConstants asyncConstants, String prefix) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(asyncConstants.getCorePoolSize());
        threadPoolTaskExecutor.setMaxPoolSize(asyncConstants.getMaxPoolSize());
        threadPoolTaskExecutor.setQueueCapacity(asyncConstants.getQueueCapacity());
        threadPoolTaskExecutor.setKeepAliveSeconds(asyncConstants.getKeepAliveSeconds());
        threadPoolTaskExecutor.setThreadNamePrefix(prefix);
        // 线程池对拒绝任务(无线程可用)的处理策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return threadPoolTaskExecutor;
    }
}
